<template>
<div style="width: 50%">
  <div style="display: flex;justify-content: flex-start;margin-bottom: 10px" >
    <el-input
        placeholder="输入部门"
        v-model="filterText"
        prefix-icon="el-icon-search"
        size="small"
        style="margin-right:5px">
    </el-input>
  </div>
  <div class="block">
    <el-tree
        :data="dataDeps"
        show-checkbox
        node-key="id"
        ref="tree"
        :props="defaultProps"
        :filter-node-method="filterNode"
        :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }" style="display: flex;justify-content: space-between;width: 100%">
        <span class="depName">{{ data.name }}</span>
        <span>
          <el-button
              type="primary"
              size="mini"
              @click="() => showAddDep(node,data)"
               class="elbutton">
           添加部门
          </el-button>
          <el-button
              type="danger"
              size="mini"
              @click="() => deleteDep(data)"
              class="elbutton">
            删除部门
          </el-button>
        </span>
      </span>
    </el-tree>
  </div>
  <el-dialog
      title="添加部门"
      :visible.sync="dialogVisible"
      width="30%">
    <div>
      <table>
        <tr>
          <td> <el-tag>上级部门</el-tag></td>
          <td>{{parentName}}</td>
        </tr>
        <tr>
          <td> <el-tag> 部门名称</el-tag></td>
          <td> <el-input size="small" v-model="dep.name"></el-input></td>
        </tr>
      </table>
    </div>
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addDep">确 定</el-button>
  </span>
  </el-dialog>


</div>
</template>

<script>
import axios from "axios";

export default {
  name: "DepMana",
  data(){
    return {
      filterText:'',
      parentName:'',
      dialogVisible:false,
      dataDeps:[],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dep:{
          name:'',
        parentId:''
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },

  methods: {
    showAddDep(nodo,data){
      this.dialogVisible = true;
      this.parentName = data.name;
      this.dep.parentId = data.id
    },
    addDep(){
     if(this.dep.name){
       axios({
         method:'post',
         url:'api/system/basic/department/',
         data:this.dep
       }).then(res=>{
         if(res){
           this.initDeps(),
           this.dialogVisible = false
         }
       })
     }else{
       this.$message.error('不要输入为空')
     }
    },
    deleteDep(data){
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios({
          method:'delete',
          url:`api/system/basic/department/${data.id}`
        }).then(res=>{
          if(res){
            this.initDeps()
          }
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    initDeps(){
      axios({
        method:'get',
        url:'api/system/basic/department/'
      }).then(res=>{
        if(res){
          this.dataDeps = res.data
        }
      })
    }
  },
  mounted() {
    this.initDeps()
  }

}
</script>

<style scoped>
.elbutton{
  padding: 2px;
}
.depName{
  margin-right: 5px;
}
</style>